﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace HCore
{
    public partial class Server
    {
        private readonly int _lobbyPlayerNum;
        private int _nextPlayerId;
        private readonly Dictionary<uint, ClientProxy> _clientProxies;
        private readonly ServerNetworkManager _networkMgr;
        private readonly PrecisionTimer _timer;
        private readonly StateMachine<Server> _stateMachine;

        public Server(int port, int playerNum, int frameRate)
        {
            _lobbyPlayerNum = playerNum;
            _nextPlayerId = 0;
            _clientProxies = new Dictionary<uint, ClientProxy>();
            _networkMgr = ServerNetworkManager.Get(port);
            _timer = new PrecisionTimer(frameRate);
            _stateMachine = new StateMachine<Server>(this);

            // 进入大厅
            _stateMachine.SetGlobalState<ServerGlobalState>();
            _stateMachine.ChangeState<ServerLobbyState>();
        }

        public void Update()
        {
            _networkMgr.Update();
            _networkMgr.Input();
            _stateMachine.Update();
            // 休息下
            Thread.Sleep(1);
        }

        private void Log(string str)
        {
            Logger.Log("[TotalTime: " + _timer.totalTime +"]["  + DateTime.Now.ToString("hh:mm:ss fff") + "][Server " + _timer.frameCount + " ] " +
                       str);
        }
    }
}